package com.dsmbase.corp.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

@Component
public class ExceptionHandler implements HandlerExceptionResolver
{
    private static final Log logs = LogFactory.getLog(ExceptionHandler.class);
            
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj,
        Exception exception)
    {
        request.setAttribute("exception", exception.toString());
        request.setAttribute("exceptionStack", exception);
        logs.error(exception.toString(), exception);
        response.setCharacterEncoding("UTF-8");  
        try {  
            PrintWriter writer = response.getWriter();  
            writer.write("{ failure:true, msg : '" + exception.getMessage() + "'}");  
            writer.flush();  
        } catch (IOException e) {  
              
        }  
        return null;
    }

}
